<?php

namespace app\admin\controller;

use think\Request;
use think\Db;

class Comment extends AdminController
{

    public function article()
    {
        $artlist = Db::name('article')->select();//文章信息
        $info = array();
        foreach ($artlist as $val){
            $com = Db::name('artcomment')->where('art_id',$val['art_id'])->count();
            $val['num'] = $com;
            $info[] = $val;
            // var_dump($com);
            // die;
        }
        // var_dump($info);
        // die;
        // $com =  Db::namse('comment');

        // var_dump($artlist);
        // die;
        return view('comment/article',[
            'artlist' => $info
        ]);

    }


    public function post()
    {
        $postlist = Db::name('post')->select();//帖子信息
        $info = array();
        foreach ($postlist as $val){
            $com = Db::name('postcomment')->where('post_id',$val['post_id'])->count();
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            // var_dump($user['username']);
            // die;
            $val['num'] = $com;
            $val['username'] = $user['username'];
            
            $info[] = $val;
            // var_dump($com);
            // die;
        }

        return view('comment/post',[
            'postlist' => $info
        ]);
    }

    //文章评论详情
    public function artdetail($id)
    {
        // var_dump($id);
        // die;
        $com = Db::name('artcomment')->where('art_id',$id)->select();
        $info = array();
        foreach ($com as $val){
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            // var_dump($user['username']);
            // die;
            $val['username'] = $user['username'];

            $info[] = $val;
            // var_dump($com);
            // die;
        }
        // var_dump($info);
        // die;
        return view('comment/artdetail',[
            'list' => $info
        ]);

    }
    public function postdetail($id)
    {
        // var_dump($id);
        // die;
        $com = Db::name('postcomment')->where('post_id',$id)->select();
        $info = array();
        foreach ($com as $val){
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            // var_dump($user['username']);
            // die;
            $val['username'] = $user['username'];

            $info[] = $val;
            // var_dump($com);
            // die;
        }
        // var_dump($info);
        // die;
        return view('comment/postdetail',[
            'list' => $info
        ]);

    }


    public function artruncom($id)
    {
        $rcom = Db::name('runcomment')->where('artcom_id',$id)->select();
        $info = array();
        foreach ($rcom as $val){
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            // var_dump($user['username']);
            // die;
            $val['username'] = $user['username'];

            $info[] = $val;
            // var_dump($com);
            // die;
        }
        // var_dump($info);
        // die;
        return view('comment/artruncom',[
            'list' => $info
        ]);
    }


    public function postruncom($id)
    {
        $rcom = Db::name('prcomment')->where('com_id',$id)->select();
        $info = array();
        foreach ($rcom as $val){
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            // var_dump($user['username']);
            // die;
            $val['username'] = $user['username'];

            $info[] = $val;
            // var_dump($com);
            // die;
        }
        // var_dump($info);
        // die;
        return view('comment/postruncom',[
            'list' => $info
        ]);
    }


    public function delprc()
    {
        $result = Db::name('prcomment')->delete($_POST['prcid']);

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);
    }

    public function delarc()
    {
        $result = Db::name('runcomment')->delete($_POST['arcid']);

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);
    }

    public function delpc()
    {

       
        $prc = Db::name('prcomment')->where('com_id',$_POST['pcid'])->select();//根据评论id 查询追评的内容
       
        if(!empty($prc)){//判断追评内容能是否为空，为空执行删除

            foreach($prc as $val){

                $result = Db::name('prcomment')->where('prc_id',$val['prc_id'])->delete();
                
            }

        }
    
        $result = Db::name('postcomment')->where('postcom_id',$_POST['pcid'])->delete();//删除评论

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }

        return json($info);

    }

    public function delac()
    {

       
        $arc = Db::name('runcomment')->where('artcom_id',$_POST['acid'])->select();//根据评论id 查询追评的内容
       
        if(!empty($arc)){//判断追评内容能是否为空，为空执行删除
            foreach($arc as $val){
                $result = Db::name('runcomment')->where('runcom_id',$val['runcom_id'])->delete();
            }
        }
        $result = Db::name('artcomment')->where('artcom_id',$_POST['acid'])->delete();//删除评论

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }

        return json($info);

    }
}
